package com.shisfish.library.Timing;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TimerTask;

import com.shisfish.library.Impl.BookLendImpl;
import com.shisfish.library.Model.Book;
import com.shisfish.library.Model.BookLend;
import com.shisfish.library.Model.BookSingle;
import com.shisfish.library.Model.User;
import com.shisfish.library.Prere.GetSysMailbox;
import com.shisfish.library.Utils.SendEmailUtils;

public class IsOverdue extends TimerTask {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		List<BookLend> bookLends = new BookLendImpl().selectOverdue();
		
		if (bookLends.size() > 0) {
			
			// 找到全部的过期用户对象
			Set<String> borrowers = new HashSet<String>();
			for (int i = 1, len = bookLends.size(); i < len; i++) {
				borrowers.add(bookLends.get(i).getBorrower());
			}
			
			// 单个用户一条数据
			List<HashMap<String, Object>> overdue = new ArrayList<HashMap<String, Object>>();
			for (String string : borrowers) {
				List<Book> books = new ArrayList<Book>();
				List<BookSingle> bookSingles = new ArrayList<BookSingle>();
				for (int i = 0, len = bookLends.size(); i < len; i++) {
					if (bookLends.get(i).getBorrower().equals(string)) {
						books.add(bookLends.get(i).getBook());
						bookSingles.add(bookLends.get(i).getBookSingle());
					}
				}
				HashMap<String, Object> temp = new HashMap<String, Object>();
				temp.put("borrowers", string);
				temp.put("books", books);
				temp.put("bookSingles", bookSingles);
				overdue.add(temp);
			}
			
			// 把用户信息添加进去
			for (HashMap<String, Object> hashMap : overdue) {
				for (int i = 0, len = bookLends.size(); i < len; i++) {
					User user = bookLends.get(i).getUser();
					if (hashMap.get("borrowers").equals(user.getEmployee())) {
						hashMap.put("user", user);
						break;
					}
				}
			}
			
			for (HashMap<String, Object> hashMap : overdue) {
				User user = (User) hashMap.get("user");
				if (!"".equals(user.getEmail())) {
					
					@SuppressWarnings("unchecked")
					ArrayList<Book> books = (ArrayList<Book>) hashMap.get("books");
					int count = books.size();
					StringBuffer sb = new StringBuffer();
					for (Book book : books) {
						sb.append("《" + book.getTitle() + "》,");
					}
					//String bookname = sb.substring(0, sb.length()-1).toString();
					
					String to = user.getEmail();
					String subject = "你有" + count + "本书过期了！";
					
					StringBuffer sb1 = new StringBuffer();
					sb1.append("<strong><center>图书馆图书到期通知</center></strong><br>");
					sb1.append("<table style='align:left;width:100%'>");
					sb1.append(" 用户名为 <b>[王大锤]</b>  的读者   您好：");
					sb1.append("<table cellpadding='0' cellspacing='0'>");
					sb1.append("<tbody><tr><td colspan='7'><b style='color:#666666;font-size: 14px;' >到期提醒</b></td></tr>");
					sb1.append("<tr>");
					sb1.append("<td style='background-color:#5F9EA0;color:#ffffff;font-size:12px;font-weight:bold;text-indent:8px ;'>题名</td>");
					sb1.append("<td style='background-color:#5F9EA0;color:#ffffff;font-size:12px;font-weight:bold;text-indent:8px ;'>作者</td>");
					//sb1.append("<td style='background-color:#5F9EA0;color:#ffffff;font-size:12px;font-weight:bold;text-indent:8px ;'>还书处</td>");
					//sb1.append("<td style='background-color:#5F9EA0;color:#ffffff;font-size:12px;font-weight:bold;text-indent:8px ;'>条码号</td>");
					sb1.append("<td style='background-color:#5F9EA0;color:#ffffff;font-size:12px;font-weight:bold;text-indent:8px ;'>分类号</td>");
					//sb1.append("<td style='background-color:#5F9EA0;color:#ffffff;font-size:12px;font-weight:bold;text-indent:8px ;'>应还日期</td>");
					sb1.append("</tr>");
					
					for (Book book : books) {
						sb1.append("<tr");
						sb1.append("<td style='background-color:#eeeeee;color:#666666;font-size:12px;text-indent:8px;border:0px solid #ffffff;'>《"+book.getTitle()+"》</td>");
						sb1.append("<td style='background-color:#eeeeee;color:#666666;font-size:12px;text-indent:8px;border:0px solid #ffffff;'>"+book.getAuthor()+"</td>");
						//sb1.append("<td style='background-color:#eeeeee;color:#666666;font-size:12px;text-indent:8px;border:0px solid #ffffff;'>>Jess Chadwick, Todd Snyder, Hrusikesh Panda著</td>");
						//sb1.append("<td style='background-color:#eeeeee;color:#666666;font-size:12px;text-indent:8px;border:0px solid #ffffff;'>>下沙馆</td>");
						sb1.append("<td style='background-color:#eeeeee;color:#666666;font-size:12px;text-indent:8px;border:0px solid #ffffff;'>"+book.getCategory()+"</td>");
						//sb1.append("<td style='background-color:#eeeeee;color:#666666;font-size:12px;text-indent:8px;border:0px solid #ffffff;'>>2015-12-03</td>");
						sb1.append("</tr>");
					}
					
					sb1.append("</tbody></table>");
					sb1.append("</table>");
					sb1.append("<div style='font-size: 12px;color: darkgray; font-weight: 100;'><font>信件内容是"+new Date()+"生成的，可能存在延时，仅作提示使用，一切信息皆以您在图书馆实际情况为准。<br>");
					sb1.append("这封信是由图书馆应用服务系统自动发送的。如有任何问题，请与我们的工作人员联系。</font></div>");
					
					/*sb1.append("你的书" + bookname + "已经过期！请尽快归还！</br>");
					sb1.append("<table style='border:1px black solid'><thead><th>书名</th><th>作者</th><th>分类号</th></thead><tbody>");
					for (Book book : books) {
						sb1.append("<tr><td>《"+book.getTitle()+"》</td><td>"+book.getAuthor()+"</td><td>"+book.getCategory()+"</td><tr>");
					}
					sb1.append("</tbody></table>");*/
					String[] str = GetSysMailbox.getSysMailbox();
					SendEmailUtils.sendEmail(to, subject, sb1.toString(), str[0], str[0], str[1]);
				}
			}
		}
		else {
			System.out.println("没有超期的书！");
		}
		
	}
	
}
